-- magic_damage
-- Created by chengb Sep/11/2015
-- 成就：迷宫中，单次魔法伤害超过{damage}点"

-- 内部函数声明
local whenAttackResult;

-- 注册玩家攻击怪物的结果事件
EventMgr.register("game_achievement/magic_damage", event.HERO_ATTACK_RESULT, function(args)
    whenAttackResult(args);
end);

-- 注册玩家攻击怪物的结果事件
whenAttackResult = function(args)
    local damage = args.damage;
    local extra  = args.extra;
    
    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end
        
        local config = GameAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["damage"]) == 'number' and
                damage < ruleArg["damage"] then
                -- 伤害未达标
                return false;
            end
        end
        
        return true;
    end
    
    -- 遍历所有的子规则
    local ids = GameAchievementM.getIdsByRule("magic_damage");
    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameAchievementM.tryComplete(id, 1);
        end
    end
end

return {
    -- 获取成就描述
    queryDesc = function(id)
        local config = GameAchievementM.query(id);

        local ruleArg  = config["rule_arg"];
        local damage   = ruleArg["damage"];
        local desc     = config["desc"];
        
        desc = string.gsub(desc, "{damage}", damage);

        return desc;
    end,
};